package com.ruoyi.project.rate.service;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.framework.web.service.CrudService;
import com.ruoyi.project.rate.RuleComparator;
import com.ruoyi.project.rate.domain.Rule;
import com.ruoyi.project.rate.mapper.RuleMapper;

@Service
public class RuleService extends CrudService<RuleMapper, Rule> {
	@Autowired
	private RuleMapper ruleMapper;
	
	@Transactional(readOnly = false)
	public void batchSave(List<Rule> ruleList) {
		if (ruleList.size() > 0) {
			Rule r = new Rule();
			r.setKhz(ruleList.get(0).getKhz());
			r.setType(ruleList.get(0).getType());
			this.ruleMapper.deleteByKhzAndType(r);
		}
		for (Rule rule : ruleList) {
			this.save(rule);
		}
	}
	
	public List<Rule> findList(Rule entity) {
		List<Rule> ruleList = super.findList(entity);
		Collections.sort(ruleList, new RuleComparator());
		return dao.findList(entity);
	}
}
